package com.wy.w_ui.banner;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.wy.w_ui.R;
import com.wy.w_ui.banner.adapter.IWBindAdapter;
import com.wy.w_ui.banner.core.IWBanner;
import com.wy.w_ui.banner.core.WBannerBean;
import com.wy.w_ui.banner.core.WBannerHelper;
import com.wy.w_ui.banner.indicator.IWIndicator;

import java.util.List;

public class WBanner extends FrameLayout implements IWBanner {

    private WBannerHelper bannerHelper;

    public WBanner(@NonNull Context context) {
        this(context, null);
    }

    public WBanner(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WBanner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        bannerHelper = new WBannerHelper(context, this);
        initCustomAttrs(context, attrs);
    }

    /**
     * 初始化自定义属性
     */
    private void initCustomAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WBanner);
        boolean autoPlay = typedArray.getBoolean(R.styleable.WBanner_autoPlay, true);
        boolean loop = typedArray.getBoolean(R.styleable.WBanner_loop, true);
        int intervalTime = typedArray.getInteger(R.styleable.WBanner_intervalTime, -1);
        setAutoPlay(autoPlay);
        setLoop(loop);
        setIntervalTime(intervalTime);
    }

    @Override
    public void setBannerData(int layoutResId, @NonNull List<? extends WBannerBean> models) {
        bannerHelper.setBannerData(layoutResId, models);
    }

    @Override
    public void setBannerData(@NonNull List<? extends WBannerBean> models) {
        bannerHelper.setBannerData(models);
    }

    @Override
    public void setIndicator(IWIndicator indicator) {
        bannerHelper.setIndicator(indicator);
    }

    @Override
    public void setAutoPlay(boolean autoPlay) {
        bannerHelper.setAutoPlay(autoPlay);
    }

    @Override
    public void setLoop(boolean loop) {
        bannerHelper.setLoop(loop);
    }

    @Override
    public void setIntervalTime(int intervalTime) {
        bannerHelper.setIntervalTime(intervalTime);
    }

    @Override
    public void setBindAdapter(IWBindAdapter bindAdapter) {
        bannerHelper.setBindAdapter(bindAdapter);
    }

    @Override
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        bannerHelper.setOnPageChangeListener(onPageChangeListener);
    }

    @Override
    public void setScroller(int duration) {
        bannerHelper.setScroller(duration);
    }

    @Override
    public void setOnBannerClickLister(OnBannerClickListener onBannerClickListener) {
        bannerHelper.setOnBannerClickLister(onBannerClickListener);
    }
}
